package com.hnkywl.sts.dao.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.kmys.Cyfyys;
import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import com.hnkywl.sts.entity.sys.Yg;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-17
 * Time: 上午9:33
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class CyfyysDao extends SimpleHibernateTemplate<Cyfyys,Long>{
    public Page<Cyfyys> findAllFy( Long gsId,String kmdm, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Cyfyys> page = new Page<Cyfyys>(pageNo, pageSize, 0);
        //==============条件拼接===============

        if (kmdm != null && !kmdm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" kmdm like ? ");
            param.add("%"+kmdm.trim()+"%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(gsId);
        }
        //==============记录数量===============
        hql = "select count(*) from Cyfyys where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Cyfyys where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }
    public void deleteByGsId(Long gsId){
        String hqlString = "delete from Cyfyys where nbgs.id = :gsId";
        Query query = getSession().createQuery(hqlString);
        query.setParameter("gsId",gsId);
        query.executeUpdate();
    }


    public Cyfyys findFyByGsIdandCodeId(Long gsId,String codeId){
        String   hql = " from  Cyfyys where nbgs.id = ? and codeId = ?";
        return  find(hql,gsId,codeId).size()>0?(Cyfyys)find(hql,gsId,codeId).get(0):new Cyfyys();
    }

    public Cyfyys getCommentSubject(Long compid,String fylb){
        String   hql = " from  Cyfyys where nbgs.id = ? and fylb = ?";
        return  find(hql,compid,fylb).size()>0?(Cyfyys)find(hql,compid,fylb).get(0):new Cyfyys();
    }

    public List<Map> findFylbCombobox(Long sydwId){
        String hql = "select new map(id as id,fykmjc as fykmjc,sl as sl) from Cyfyys where nbgs.id = ?";
        return find(hql,sydwId);
    }
}
